<template>
  <div class="hospitalsign">
    <div class="top">
      <div class="title">京外就医备案</div>
      <div class="topinfo" :class="{'has-arrow': !isReadonly}">
        <div class="post">开始时间</div>
        <div :class="{'empty-style': !form.kssj}" @click="!isReadonly && showDatePicker('kssj')">{{form.kssj | formatDate}}</div>
      </div>
      <div class="topinfo" :class="{'has-arrow': !isReadonly}">
        <div class="post">结束时间</div>
        <div :class="{'empty-style': !form.jssj}" @click="!isReadonly && showDatePicker('jssj')">{{form.jssj | formatDate}}</div>
      </div>
      <div class="topinfo">
        <div class="post">出京事由</div>
        <div><input
            type="text"
            placeholder="请输入出京事由"
            :readonly="isReadonly"
            v-model="form.cjsy"
          /></div>
      </div>
      <div class="topinfo">
        <div class="post">目的地</div>
        <div><input
            type="text"
            :readonly="isReadonly"
            placeholder="请输入目的地"
            v-model="form.mdd"
          /></div>
      </div>
      <div class="topinfo">
        <div class="post">异地医院名称</div>
        <div><input
            type="text"
            :readonly="isReadonly"
            placeholder="请输入异地医院名称"
            v-model="form.ydyymc"
          /></div>
      </div>
      <!-- <div class="topinfo">
                <div class="post special">是否需要支票（2-10万）</div>
                <div class="arrow" @click="showPicker('needzp')">
                    <p :style="{'color': needzp?'#333333' :'#cccccc'}">{{needzp ? needzp : '请选择'}}</p>
                    <van-icon name="arrow" />
                </div>
            </div> -->
    </div>
    <div class="sign-btn" v-if="!isReadonly" @click="submit">提交</div>
    <div class="historylist">
      <p class="historytitle">就医记录</p>
      <div @click="loadDetail(item.jwjybaId)" class="item" v-for="(item, i) in list" :key="i">
        <div>
          <p>就医日期：{{ item.kssj }}</p>
          <p>{{ item.ydyymc }}</p>
        </div>
        <div><van-icon name="arrow" /></div>
      </div>
    </div>
    <date-picker
      @fetchData="fetchDate"
      date-type="date"
      :show="isDatePickerShow"
      @cancel="hideDatePicker"
      @hide="hideDatePicker"
    ></date-picker>
  </div>
</template>

<script>
import datePicker from '../components/datePicker';
import {jwjybaSavedata, jwjybaQuerydata4list, jwjybaQuerydata4object} from '@/assets/js/api';
import scrollToTop from '@/assets/js/mixins/scrollToTop';
import moment from 'moment';
export default {
  filters: {
    formatDate(v) {
      return (v && moment(v).format('YYYY-MM-DD')) || '请选择';
    }
  },
  mixins: [scrollToTop],
  computed: {
    isReadonly() {
      return !!this.$route.query.id;
    }
  },
  data() {
    return {
      dateType: '',
      isDatePickerShow: false,
      form: {
        kssj: '',
        jssj: '',
        cjsy: '',
        mdd: '',
        ydyymc: ''
      },
      list: [],
      dspToken: localStorage.dspToken
    };
  },
  components: {
    datePicker
  },
  mounted() {
    this.loadList();
  },
  methods: {
    loadList() {
      jwjybaQuerydata4list({
        dspToken: this.dspToken
      }).then(res => {
        this.list = res.data || [];
      });
    },
    loadDetail(id) {
      if (id) {
        jwjybaQuerydata4object({
          jwjybaId: id,
          dspToken: this.dspToken
        }).then(({data}) => {
          const o = data[0];
          this.$router.push({
            name: 'detail',
            query: {
              title: '京外就医备案详情',
              data: JSON.stringify({
                '开始时间': o.kssj,
                '结束时间': o.jssj,
                '出京事由': o.cjsy,
                '目的地': o.mdd,
                '异地医院名称': o.ydyymc,
                '提交时间': o.createTime
              })
            }
          });
        });
      }
    },
    showDatePicker(type) {
      this.isDatePickerShow = true;
      this.dateType = type;
    },
    fetchDate(d) {
      this.form[this.dateType] = d.originValue;
      this.isDatePickerShow = d.showDatePicker;
    },
    hideDatePicker(d) {
      this.isDatePickerShow = d.showDatePicker;
    },
    submit() {
      if (!this.form.kssj) {
        this.$toast('请选择开始时间');
        return;
      }
      if (!this.form.jssj) {
        this.$toast('请选择结束时间');
        return;
      }
      if (!this.form.cjsy) {
        this.$toast('请填写出京事由');
        return;
      }
      if (!this.form.mdd) {
        this.$toast('请填写目的地');
        return;
      }
      if (!this.form.ydyymc) {
        this.$toast('请填写异地医院名称');
        return;
      }
      jwjybaSavedata({
        ...this.form,
        kssj: moment(this.form.kssj).format('YYYY-MM-DD'),
        jssj: moment(this.form.jssj).format('YYYY-MM-DD'),
        dspToken: this.dspToken
      }).then(({pkValue}) => {
        this.$toast('提交成功');
        this.loadList();
        this.form = {
          kssj: '',
          jssj: '',
          cjsy: '',
          mdd: '',
          ydyymc: ''
        };
      });
    }
  }
};
</script>

<style lang="less" scoped>
.hospitalsign {
  padding: 24px;
  .top {
    width: 704px;
    background-color: #ffffff;
    box-shadow: 3px 4px 9px 1px rgba(1, 88, 155, 0.05);
    border-radius: 8px;
    margin: 0 auto;
    .title {
      height: 64px;
      background-color: #3198ed;
      color: #ffffff;
      font-size: 30px;
      line-height: 64px;
      padding: 0 16px;
      border-top-left-radius: 8px;
      border-top-right-radius: 8px;
    }
    .topinfo {
      display: flex;
      padding: 35px 18px;
      box-sizing: border-box;
      width: 674px;
      margin: 0 auto;
      font-size: 34px;
      color: #343434;
      &:not(:last-child) {
        border-bottom: 1px solid #eeeeee;
      }
      .post {
        color: #888888;
        text-align: left;
        width: 220px;
      }
      div:last-child {
        flex: 1;
        text-align: right;
      }
      .empty-style{
        color: #bfbfbf;
      }
      .special {
        width: 380px;
      }
      &.has-arrow {
        position: relative;
        padding-right: 32px;
        &::before {
          content: "\F00A";
          position: absolute;
          right: 0;
          top: 50%;
          transform: translateY(-50%);
          font: normal normal normal 1.867vw/1 vant-icon;
          color: #b2b2b2;
          font-size: inherit;
          text-rendering: auto;
          -webkit-font-smoothing: antialiased;
        }
      }
      input{
        text-align: right;
      }
    }
  }
  .sign-btn {
    border: none;
    width: 704px;
    height: 90px;
    background-color: #3198ed;
    box-shadow: 0px 5px 5px 0px rgba(89, 166, 250, 0.1);
    border-radius: 45px;
    line-height: 90px;
    color: #ffffff;
    font-size: 36px;
    text-align: center;
    margin: 26px auto;
    letter-spacing: 7px;
  }
  .historylist {
    .historytitle {
      color: #888888;
      font-size: 30px;
      margin: 19px 0;
    }
    .item {
      width: 704px;
      // height: 140px;
      background-color: #ffffff;
      box-shadow: 3px 4px 9px 1px rgba(1, 88, 155, 0.05);
      border-radius: 8px;
      margin: 0 auto;
      margin-top: 23px;
      position: relative;
      padding: 31px 30px 26px 33px;
      box-sizing: border-box;
      display: flex;
      &::before {
        content: "";
        width: 5px;
        // height: 140px;
        background-color: #3198ed;
        position: absolute;
        left: 0;
        top: 0;
        bottom: 0;
        border-top-left-radius: 50px;
        border-bottom-left-radius: 50px;
      }
      > div:first-child {
        flex: 1;
        p:first-child {
          color: #888888;
          font-size: 30px;
        }
        p:last-child {
          width: calc(100vw - 160px);
          color: #353535;
          font-size: 34px;
          // white-space: nowrap;
          // text-overflow: ellipsis;
          // overflow: hidden;
        }
      }
      > div:last-child {
        width: 50px;
        font-size: 34px;
        color: #b2b2b2;
        align-self: center;
        text-align: right;
      }
    }
  }
}
</style>
